package com.blackrock.das.service.marshalling;

import org.springframework.stereotype.Service;

import com.blackrock.das.web.converters.CustomMappingJackson2HttpMessageConverter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializationFeature;

/**
 * The Class JsonMarshallingServiceImpl.
 */
@Service
public class JsonMarshallingServiceImpl implements JsonMarshallingService {

    /** The jackson message converter. */
    private CustomMappingJackson2HttpMessageConverter jacksonMessageConverter = new CustomMappingJackson2HttpMessageConverter(false);

    /*
     * (non-Javadoc)
     * 
     * @see com.blackrock.ebiz.dal.server.service.marshalling.json.JsonMarshallingService#marshall(java.lang.Object)
     */
    @Override
    public String marshall(Object obj) throws JsonProcessingException {
        return this.jacksonMessageConverter.getObjectMapper().writeValueAsString(obj);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.blackrock.ebiz.dal.server.service.marshalling.json.JsonMarshallingService#marshall(java.lang.Object, boolean)
     */
    @Override
    public String marshall(Object obj, boolean prettyPrint) throws JsonProcessingException {
        if (prettyPrint) {
            return this.jacksonMessageConverter.getObjectMapper().writer(SerializationFeature.INDENT_OUTPUT).writeValueAsString(obj);
        } else {
            return marshall(obj);
        }
    }

}
